package com.lusoft.onepiece.monster;

import org.andengine.engine.handler.physics.PhysicsHandler;
import org.andengine.entity.IEntity;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.lusoft.onepiece.constant.GameConstant;
import com.lusoft.onepiece.util.GameUtil;

public class Monster extends AnimatedSprite {

	private PhysicsHandler physicsHandler;
	
	public Monster(float x, float y, ITiledTextureRegion region, VertexBufferObjectManager vbom){
		super(x, y, region, vbom);
		physicsHandler = new PhysicsHandler(this){
			@Override
			protected void onUpdate(float pSecondsElapsed, IEntity pEntity) {
				super.setVelocityY(super.getVelocityY() + GameConstant.Velocity.GRAVITY);
				super.setVelocityX(super.getVelocityX() + GameUtil.randomSpeed());
				super.onUpdate(pSecondsElapsed, pEntity);
			}
		};
		physicsHandler.setVelocity(GameConstant.Velocity.DEFAULT_X, GameConstant.Velocity.DEFAULT_Y);
		this.registerUpdateHandler(physicsHandler);
	}

	@Override
	protected void onManagedUpdate(float pSecondsElapsed) {
		super.onManagedUpdate(pSecondsElapsed);
		if(physicsHandler.getVelocityX() > 0 && this.getX() > (480 - physicsHandler.getVelocityX())){
			physicsHandler.setVelocityX(-physicsHandler.getVelocityX());
		}
	}
	
	
}
